/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2018-2019. All rights reserved.
 *
 * @file    msg_venc.c
 * @brief   msg venc function
 * @author  HiMobileCam NDK develop team
 * @date  2019-3-26
 */
#include "hi_type.h"
#include "hi_ipcmsg.h"
#include "msg_define.h"
#include "msg_osd.h"
#include "hi_datafifo.h"
#include "hi_mapi_osd.h"
#include "hi_mapi_osd_define.h"
#include "msg_server.h"
#include "mapi_comm_inner.h"
#include "hi_mapi_log.h"

#ifdef __cplusplus
#if __cplusplus
extern "C" {
#endif
#endif /* End of #ifdef __cplusplus */


static HI_S32 MSG_OSD_Deinit(HI_S32 siId, HI_IPCMSG_MESSAGE_S *msg)
{
    HI_S32 ret;

    ret = HI_MAPI_OSD_Deinit();
    if (ret != HI_SUCCESS) {
        MAPI_ERR_TRACE(HI_MAPI_MOD_OSD, "HI_MAPI_OSD_Deinit Failed : %#x!\n", ret);
    }

    HI_IPCMSG_MESSAGE_S *respMsg = HI_IPCMSG_CreateRespMessage(msg, ret, HI_NULL, 0);
    if (respMsg == HI_NULL) {
        MAPI_ERR_TRACE(HI_MAPI_MOD_OSD, "OSD_Deinit call HI_IPCMSG_CreateRespMessage fail\n");
    }

    ret = HI_IPCMSG_SendAsync(siId, respMsg, HI_NULL);
    if (ret != HI_SUCCESS) {
        MAPI_ERR_TRACE(HI_MAPI_MOD_OSD, "OSD_Deinit call HI_IPCMSG_SendAsync fail,ret:%x\n", ret);
    }

    HI_IPCMSG_DestroyMessage(respMsg);
    return ret;
}

static HI_S32 MSG_OSD_Start(HI_S32 siId, HI_IPCMSG_MESSAGE_S *pstMsg)
{
    HI_HANDLE OsdHdl;
    OsdHdl = GET_CHN_ID(pstMsg->u32Module);
    HI_S32 s32Ret;
    HI_IPCMSG_MESSAGE_S *respMsg = HI_NULL;

    s32Ret = HI_MAPI_OSD_Start(OsdHdl);
    if (s32Ret != HI_SUCCESS) {
        MAPI_ERR_TRACE(HI_MAPI_MOD_OSD, "HI_MAPI_OSD_Start Failed : %#x!\n", s32Ret);
    }

    respMsg = HI_IPCMSG_CreateRespMessage(pstMsg, s32Ret, HI_NULL, 0);
    if (respMsg == HI_NULL) {
        MAPI_ERR_TRACE(HI_MAPI_MOD_OSD, "OSD_Start call HI_IPCMSG_CreateRespMessage fail\n");
    }

    s32Ret = HI_IPCMSG_SendAsync(siId, respMsg, HI_NULL);
    if (s32Ret != HI_SUCCESS) {
        MAPI_ERR_TRACE(HI_MAPI_MOD_OSD, "OSD_Start call HI_IPCMSG_SendAsync fail,ret:%x\n", s32Ret);
    }

    HI_IPCMSG_DestroyMessage(respMsg);
    return s32Ret;
}


static HI_S32 MSG_OSD_Stop(HI_S32 siId, HI_IPCMSG_MESSAGE_S *pstMsg)
{
    HI_HANDLE OsdHdl;
    OsdHdl = GET_CHN_ID(pstMsg->u32Module);
    HI_S32 s32Ret;
    HI_IPCMSG_MESSAGE_S *respMsg = HI_NULL;

    s32Ret = HI_MAPI_OSD_Stop(OsdHdl);
    if (s32Ret != HI_SUCCESS) {
        MAPI_ERR_TRACE(HI_MAPI_MOD_OSD, "HI_MAPI_OSD_Stop Failed : %#x!\n", s32Ret);
    }

    respMsg = HI_IPCMSG_CreateRespMessage(pstMsg, s32Ret, HI_NULL, 0);
    if (respMsg == HI_NULL) {
        MAPI_ERR_TRACE(HI_MAPI_MOD_OSD, "OSD_Stop call HI_IPCMSG_CreateRespMessage fail\n");
    }

    s32Ret = HI_IPCMSG_SendAsync(siId, respMsg, HI_NULL);
    if (s32Ret != HI_SUCCESS) {
        MAPI_ERR_TRACE(HI_MAPI_MOD_OSD, "OSD_Stop call HI_IPCMSG_SendAsync fail,ret:%x\n", s32Ret);
    }

    HI_IPCMSG_DestroyMessage(respMsg);
    return s32Ret;
}


static HI_S32 MSG_OSD_SetAttr(HI_S32 siId, HI_IPCMSG_MESSAGE_S *pstMsg)
{
    HI_HANDLE OsdHdl;
    OsdHdl = GET_CHN_ID(pstMsg->u32Module);
    HI_S32 s32Ret;
    HI_IPCMSG_MESSAGE_S *respMsg = HI_NULL;

    HI_MAPI_OSD_ATTR_S *pOsdAttr = (HI_MAPI_OSD_ATTR_S *)pstMsg->pBody;
    s32Ret = HI_MAPI_OSD_SetAttr(OsdHdl, pOsdAttr);
    if (s32Ret != HI_SUCCESS) {
        MAPI_ERR_TRACE(HI_MAPI_MOD_OSD, "Failed : %#x!\n", s32Ret);
    }

    respMsg = HI_IPCMSG_CreateRespMessage(pstMsg, s32Ret, HI_NULL, 0);
    if (respMsg == HI_NULL) {
        MAPI_ERR_TRACE(HI_MAPI_MOD_OSD, "OSD_SetAttr call HI_IPCMSG_CreateRespMessage fail\n");
    }

    s32Ret = HI_IPCMSG_SendAsync(siId, respMsg, HI_NULL);
    if (s32Ret != HI_SUCCESS) {
        MAPI_ERR_TRACE(HI_MAPI_MOD_OSD, "OSD_SetAttr call HI_IPCMSG_SendAsync fail,ret:%x\n", s32Ret);
        HI_IPCMSG_DestroyMessage(respMsg);
        return s32Ret;
    }

    HI_IPCMSG_DestroyMessage(respMsg);
    return HI_SUCCESS;
}

static HI_S32 MSG_OSD_GetAttr(HI_S32 siId, HI_IPCMSG_MESSAGE_S *pstMsg)
{
    HI_HANDLE OsdHdl;
    OsdHdl = GET_CHN_ID(pstMsg->u32Module);
    HI_S32 s32Ret;
    HI_IPCMSG_MESSAGE_S *respMsg = HI_NULL;
    HI_MAPI_OSD_ATTR_S stAttr;

    s32Ret = memset_s(&stAttr, sizeof(HI_MAPI_OSD_ATTR_S), 0x0, sizeof(HI_MAPI_OSD_ATTR_S));
    MAPI_CHECK_PRINT(HI_MAPI_MOD_OSD, "memset_s", s32Ret);
    s32Ret = HI_MAPI_OSD_GetAttr(OsdHdl, &stAttr);
    if (s32Ret != HI_SUCCESS) {
        MAPI_ERR_TRACE(HI_MAPI_MOD_OSD, "Failed : %#x!\n", s32Ret);
    }

    respMsg = HI_IPCMSG_CreateRespMessage(pstMsg, s32Ret, &stAttr, sizeof(HI_MAPI_OSD_ATTR_S));
    if (respMsg == HI_NULL) {
        MAPI_ERR_TRACE(HI_MAPI_MOD_OSD, "OSD_GetAttr call HI_IPCMSG_CreateRespMessage fail\n");
    }

    s32Ret = HI_IPCMSG_SendAsync(siId, respMsg, HI_NULL);
    if (s32Ret != HI_SUCCESS) {
        MAPI_ERR_TRACE(HI_MAPI_MOD_OSD, "OSD_GetAttr call HI_IPCMSG_SendAsync fail,ret:%x\n", s32Ret);
        HI_IPCMSG_DestroyMessage(respMsg);
        return s32Ret;
    }

    HI_IPCMSG_DestroyMessage(respMsg);
    return HI_SUCCESS;
}

static HI_S32 MSG_OSD_Batch(HI_S32 siId, HI_IPCMSG_MESSAGE_S *pstMsg)
{
    HI_HANDLE OsdHdl;
    OsdHdl = GET_CHN_ID(pstMsg->u32Module);
    HI_S32 s32Ret;
    HI_IPCMSG_MESSAGE_S *respMsg = HI_NULL;
    s32Ret = HI_MAPI_OSD_Batch(OsdHdl, *((HI_BOOL *)pstMsg->pBody));
    if (s32Ret != HI_SUCCESS) {
        MAPI_ERR_TRACE(HI_MAPI_MOD_OSD, "HI_MAPI_OSD_Batch Failed : %#x!\n", s32Ret);
    }

    respMsg = HI_IPCMSG_CreateRespMessage(pstMsg, s32Ret, HI_NULL, 0);
    if (respMsg == HI_NULL) {
        MAPI_ERR_TRACE(HI_MAPI_MOD_OSD, "OSD_Batch call HI_IPCMSG_CreateRespMessage fail\n");
    }

    s32Ret = HI_IPCMSG_SendAsync(siId, respMsg, HI_NULL);
    if (s32Ret != HI_SUCCESS) {
        MAPI_ERR_TRACE(HI_MAPI_MOD_OSD, "OSD_Batch call HI_IPCMSG_SendAsync fail,ret:%x\n", s32Ret);
        HI_IPCMSG_DestroyMessage(respMsg);
        return s32Ret;
    }

    HI_IPCMSG_DestroyMessage(respMsg);
    return HI_SUCCESS;
}

static MSG_MODULE_CMD_S g_stModuleCmdTable[] = {
    { MSG_CMD_OSD_DEINIT,              MSG_OSD_Deinit },
    { MSG_CMD_OSD_START,               MSG_OSD_Start },
    { MSG_CMD_OSD_STOP,                MSG_OSD_Stop },
    { MSG_CMD_OSD_SET_ATTR,            MSG_OSD_SetAttr },
    { MSG_CMD_OSD_GET_ATTR,            MSG_OSD_GetAttr },
    { MSG_CMD_OSD_BATCH,               MSG_OSD_Batch },
};

MSG_SERVER_MODULE_S g_stModuleOsd = {
    HI_MAPI_MOD_OSD,
    "osd",
    sizeof(g_stModuleCmdTable) / sizeof(MSG_MODULE_CMD_S),
    &g_stModuleCmdTable[0]
};

MSG_SERVER_MODULE_S *MAPI_MSG_GetOsdMod(HI_VOID)
{
    return &g_stModuleOsd;
}

#ifdef __cplusplus
#if __cplusplus
}
#endif
#endif /* End of #ifdef __cplusplus */
